<?php
namespace app\service;
use app\service\VolcengineService;
use think\Lang;

class GenerateorService
{
    private $_current_lang;

    public function __construct($current_lang)
    {
        $this->_current_lang = $current_lang;
    }

    private function buildPrompt($keyword) {
        $prompt = Lang::get('ai_prompt');
        $prompt = str_replace(':keyword', $keyword, $prompt);
        $available_langs = config('available_langs');
        $lang_name = $available_langs[$this->_current_lang];
        if ($lang_name == 'en') {
            $lang_name = 'If it is Chinese lyrics, please convert it into Chinese Pinyin output. Pinyin does not need to mark the tone. If it is English, just output it directly.';
        }
        $prompt = str_replace(':language_name', $lang_name, $prompt);
        return $prompt;
    }

    function run($keyword){
        
        $config = config('volcengine');
        $volcengine = new VolcengineService($config['api_key'],$config['model'],$config['timeout']);

        $prompt = $this->buildPrompt($keyword);
        $params = [
            'role' => 'user',
            'content' => $prompt
        ];
        $result = $volcengine->call($params);
        // debug_dump($result);
        return $result;
    }
    
}